package com.comtime.usercenter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.comtime.greendaodemo.AdminInfo;
import com.comtime.greendaodemo.DeviceInfo;
import com.comtime.greendaoutils.DataBaseUtil;
import com.comtime.service.MyService;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartbox.HBApplication;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.LogUtils;
import com.comtime.utils.MySharedPreferences;
import com.comtime.utils.Util;

import java.util.ArrayList;

/**
 * Created by MyPC on 2017/8/15.
 */

public class RemoteDeleteUserActivity extends BaseActivity {
    private String TAG = "RemoteDeleteUserActivity";
    ArrayList<DeviceInfo> deviceInfos ;
    ArrayList<String> phones;
    Spinner phone;
    Spinner mBox;
    String selectedMac;
    MySharedPreferences mySharedPreferences;
    HBApplication application;
    DataBaseUtil dataBaseUtil;

    ArrayAdapter<String> phoneAdapter;
    boolean isProgressing;
    private String selectedPhone;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    deviceInfos.clear();
                    phones.clear();
                    deviceInfos= (ArrayList<DeviceInfo>) dataBaseUtil.showDeviceInfoByMac(selectedMac);
                    for (DeviceInfo info : deviceInfos) {
                        phones.add(info.getPhoneNum());

                    }
                    phone.setAdapter(phoneAdapter);
                    break;

            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remote_delete_child);
        mySharedPreferences = MySharedPreferences.getInstance(this);
        application = (HBApplication) getApplication();
        dataBaseUtil = DataBaseUtil.getInstance(this);
        init();


    }



    void init() {
        phone = (Spinner) findViewById(R.id.spinner_phone);
        mBox = (Spinner) findViewById(R.id.spinner_box);
        ArrayList<AdminInfo> adminInfos = new ArrayList<AdminInfo>();
        deviceInfos = new ArrayList<DeviceInfo>();
        final ArrayList<String> mMac = new ArrayList<String>();
        phones = new ArrayList<String>();
        adminInfos = (ArrayList<AdminInfo>) dataBaseUtil.ShowAdminInfoByPhone(mySharedPreferences.getPhoneNum());

        for (AdminInfo info : adminInfos) {
            mMac.add(info.getMac());
        }

        ArrayAdapter<String> boxAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mMac);
        mBox.setAdapter(boxAdapter);
        mBox.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedMac = mMac.get(position);

                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putString("mac",selectedMac);
                msg.setData(bundle);
                msg.what=1;
                handler.sendMessage(msg);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        
        phoneAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,phones);

        phone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedPhone = phones.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    public void BackAction(View v) {
        finish();
        overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
    }







    ;

    /**
     * 自定义提示
     *
     * @param str
     */
    Toast toast = null;

    void showToast(String str) {
        if (toast != null) {
            toast.cancel();
        }
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.toastshow, null);
        TextView textView = (TextView) view.findViewById(R.id.toastshow);
        textView.setText(str);
        toast = new Toast(this);
        double d = getResources().getDisplayMetrics().density;
        toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }
}
